// component/userauth/index.js
import urls from '../../utils/urls';
const app = getApp();

Component({
    // lifetimes: {
    attached: function() {
        var that = this;
        let userinfo = wx.getStorageSync('userinfo');
        if (!userinfo || !userinfo.mobile) {
            that.setData({
                showAuthBox: true,
            });
        }


        wx.login({
            success: (res) => {
                console.log(res);
                that.setData({
                    code: res.code
                })
            }
        })
        // }
    },
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        showAuthBox: false,
        showMask: false,
        code: ''
    },

    /**
     * 组件的方法列表
     */
    methods: {
        targetMask: function() {
            let showMask = !this.data.showMask;
            this.setData({
                showMask: showMask,
            });
        },
        show: function() {

        },

        getUserPhone: function(e) { //获取信息
            let that = this;
            console.log(e);
            wx.checkSession({
                success: (res) => {
                    app.request({
                        url: 'weixinauth/bindPhone',
                        data: {
                            code: that.data.code,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv
                        },
                        sCallback: (loginRes) => {
                            if (loginRes.code == 1) {
                                let loginInfo = wx.getStorageSync('login');
                                loginInfo.mobile = loginRes.data;
                                wx.setStorageSync('login', loginInfo);

                                that.setData({
                                    showAuthBox: false,
                                    showMask: false
                                });
                            } else {
                                app.alert(loginRes.msg);
                                wx.login({
                                    success: (res) => {
                                        console.log(res);
                                        that.setData({
                                            code: res.code
                                        })
                                    }
                                })
                            }
                        }
                    })
                },
                fail: (err) => {
                    wx.login({
                        success: res => {
                            that.data.code = res.code
                        }
                    })
                }
            })

        },
    }
})